<?php
session_start ();
// 判断用户是否已经登录
if (! isset ( $_SESSION ["userinfo"] ) and empty ( $_SESSION ["userinfo"] ["userid"] )) {
	header ( "location:../index.php" );
	session_unset ();
	session_destroy ();
	exit ();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Language" content="zh-CN">
<!-- Add jQuery library -->
<script type="text/javascript" src="../tools/lib/jquery-1.10.2.min.js"></script>
<!-- Add fancyBox main JS and CSS files -->
<script type="text/javascript"
	src="../tools/source/jquery.fancybox.pack.js?v=2.1.5"></script>
<link rel="stylesheet" type="text/css"
	href="../tools/source/jquery.fancybox.css?v=2.1.5" media="screen" />

<script type="text/javascript">
	$(document).ready(function() {
		$('.fancybox').fancybox();
	});
</script>
<link href="./Resource/Style/base.yy.css" rel="stylesheet"
	type="text/css">
<link href="./Resource/Style/info.yy.css" rel="stylesheet"
	type="text/css">
</head>
<body>
<?php
require_once '../tools/SqlHelper.class.php';
if (isset ( $_POST ['user_submit'] ) and $_POST ['user_submit'] == "上传") {
	if ((($_FILES ["user_image"] ["type"] == "image/gif") || ($_FILES ["user_image"] ["type"] == "image/jpeg")) && ($_FILES ["user_image"] ["size"] < 4000000)) {
		if ($_FILES ["user_image"] ["error"] > 0) {
			// echo "出现错误，错误编码为: " . $_FILES ["user_image"] ["error"] . "<br />";
		} else {
			$user_id = $_SESSION ["userinfo"] ["userid"];
			// $user_id = "xufeng520";
			$path = "../PersonalPhoto/" . $user_id . "/";
			if (! file_exists ( $path )) {
				// 检查是否有该文件夹，如果没有就创建，并给予最高权限
				mkdir ( $path, 0700 );
			}
			if (file_exists ( $path . $_FILES ["user_image"] ["name"] )) {
				// echo $_FILES ["user_image"] ["name"] . " 已经存在了. ";
				$user_image_name = "-1";
			} else {
				$data = gettimeofday ();
				$name = "x";
				foreach ( $data as $key => $value ) {
					$name = $name . $value;
				}
				if ($_FILES ["user_image"] ["type"] == "image/gif") {
					$user_image_name = $name . ".gif";
				} else {
					$user_image_name = $name . ".jpg";
				}
				move_uploaded_file ( $_FILES ["user_image"] ["tmp_name"], $path . $user_image_name );
				// 文件信息
				$sqlHelper = new SqlHelper ();
				$sql = "INSERT INTO `auxiliary_tb` (`user_id`, `ax_property`, `ax_value`) VALUES ('$user_id', 'photo', '$user_image_name')";
				$res = $sqlHelper->execute_dql ( $sql );
				$sqlHelper->close_connect ();
			}
		}
	} else {
		$user_image_name = "-2"; // echo "无效的文件";
	}
} else if (isset ( $_GET ['_id'] )) {
	$_id = $_GET ['_id'];
	$sql = "DELETE FROM `auxiliary_tb` WHERE `auxiliary_id`='$_id'";
	$sqlHelper = new SqlHelper ();
	$res = $sqlHelper->execute_dql ( $sql );
	$sqlHelper->close_connect ();
}
?>
	<nav>
		<div class="info_nav">
			<ul>
				<li><a href="./baseData.php">基本资料</a></li>
				<li><a href="./Details.php">详细资料</a></li>
				<li><a href="./soliloquy.php">内心独白</a></li>
				<li><a href="./uploadingPhoto.php">上传头像</a></li>
				<li><a class="a_out" href="#">我的相册</a></li>
				<li><a href="./PersonalCondition.php">征友条件</a></li>
				<li><a href="./changePassword.php">修改密码</a></li>
			</ul>
		</div>
	</nav>
	<div class="info_con">
		<div class="pic photo photo_pic">
			<form action="./myPhoto.php" method="post"
				enctype="multipart/form-data">
			<?php
			$user_id = $_SESSION ["userinfo"] ["userid"];
			// $user_id = "xufeng520";
			$sqlHelper = new SqlHelper ();
			$sql = "select ax_value,auxiliary_id from `auxiliary_tb` where `user_id`='$user_id' and `ax_property` = 'photo'";
			$res = $sqlHelper->execute_dql ( $sql );
			$path = "../PersonalPhoto/" . $user_id . "/";
			while ( $row = mysql_fetch_assoc ( $res ) ) {
				echo "<p class='userPhoto'>";
				echo "<a class='fancybox' href='" . $path . $row ['ax_value'] . "' data-fancybox-group='gallery' title='Show Love'>";
				echo "<img title='浏览图片' class='showPhoto' width='150' height='150' src='" . $path . $row ['ax_value'] . "'/>";
				echo "</a>";
				echo "<span class='img_checking'>照片审核中</span>";
				echo "<font>";
				echo "<a class='delete deleteImg' href='./myPhoto.php?_id=" . $row ['auxiliary_id'] . "'>删除</a>";
				echo "<a class='fancybox' href='" . $path . $row ['ax_value'] . "' data-fancybox-group='gallery' title='Show Love'>预览</a>";
				echo "</font>";
				echo "</p>";
			}
			$sqlHelper->close_connect ();
			?>
				<p>
					<a href="#"><img width="126" height="126" class="uploadImg"
						src="./Resource/Images/none_pic.jpg"></a><input type="file"
						name="user_image" value=""
						style="width: 50px; height: 22px; background-color: #01CBD3; border: 0; margin-top: 16px;" />
					<input class="video" name="user_submit" type="submit" value="上传"
						style="width: 50px; height: 22px; background-color: #01CBD3; border: 0; margin: 16px 0 0 10px;" />
				</p>
			</form>
		</div>
	</div>
</body>
</html>